package Text01.Text01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Text {
    /**
     * 3:在控制台输入一个学生的全部信息，保存到数据库，
     * 注意日期的处理(PreparedStatement)
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name, sex, telephone, qq, wechat,fromcity,address,idcardpic;
        Integer gradeid,pass;
        String enterdate,birthday;
        System.out.println("输入名字");
        name = input.next();
        System.out.println("输入性别");
        sex = input.next();
        System.out.println("输入手机号");
        telephone = input.next();
        System.out.println("输入qq");
        qq = input.next();
        System.out.println("输入微信");
        wechat = input.next();
        System.out.println("输入年级");
        gradeid = input.nextInt();
        System.out.println("输入入学日期");
        enterdate = input.next();
        System.out.println("输入来自哪里");
        fromcity = input.next();
        System.out.println("输入生日");
        birthday = input.next();
        System.out.println("输入地址");
        address = input.next();
        System.out.println("输入密码");
        pass = input.nextInt();
        Text t = new Text();
        if (t.add(name, sex, telephone, qq, wechat, gradeid, enterdate, fromcity, birthday, address, pass)>0){
            System.out.println("插入成功");
        }else {
            System.out.println("插入失败");
        }
    }
    public int add(String name,String sex,String telephone,String qq,String wechat,Integer gradeid,String enterdate,String fromcity,String birthday,String address,Integer pass){
        String url="jdbc:mysql://localhost:3306/kgc76?useSSL=false";
        String user = "root";
        String pwd = "root";
        Connection con = null;
        Statement statement = null;
        Integer result = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url,user,pwd);
            statement = con.createStatement();
            result = statement.executeUpdate("INSERT INTO `kgc76`.`s_student` (`sname`, `sex`, `telephone`, `qq`, `wechat`, `gradeid`, `enterdate`, `fromcity`, `birthday`, `address`, `pass`, `idcardpic`) VALUES ('"+name+"', '"+sex+"', '"+telephone+"', '"+qq+"', '"+wechat+"', "+gradeid+", '"+enterdate+"', '"+fromcity+"', '"+birthday+"', '"+address+"', '"+pass+"', "+null+");");
            if (result>0){
                return result;
            }else {
                return 0;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
}
